import json
import os


def Singleton(cls):
    """单例模式修饰器函数"""
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton

@Singleton  # 设置为单例模式
class Parameter:

    def __init__(self, path):
        self.path = path
        with open(self.path, 'r') as f:
            self.parameter = json.loads(f.read())

    def getParameter(self):

        return self.parameter

    # 目前每个修改都调用一次，耗费资源，后面可以考虑当全部修改完毕后发个信号过来，再更新
    def __update(self):
        with open(self.path, 'w') as f:
            json.dump(self.parameter, f)

    def getIP(self):
        return self.parameter['ip']

    def getPort(self):
        return self.parameter['port']

    def setIP(self, ip):
        self.parameter['ip'] = ip
        self.__update()

    def setPort(self, port):
        self.parameter['port'] = str(port)
        self.__update()

    def getTemperFrontActual(self):
        return self.parameter["temperFrontActual"]

    def getTemperMidActual(self):
        return self.parameter["temperMidActual"]

    def getTemperBackActual(self):
        return self.parameter["temperBackActual"]

    def getSpeedActual(self):
        return self.parameter["speedActual"]

    def getTorque(self):
        return self.parameter["torque"]

    def getMeltTemper(self):
        return self.parameter["meltTemper"]

    def getTemperFrontSet(self):
        return self.parameter["temperFrontSet"]

    def getTemperMidSet(self):
        return self.parameter["temperMidSet"]

    def getTemperBackSet(self):
        return self.parameter["temperBackSet"]

    def getSpeedSet(self):
        return self.parameter["speedSet"]

    def getHeatingOn(self):
        return self.parameter["heating_on"]

    def getRotorOn(self):
        return self.parameter["rotor_on"]


    def MAX_TEMPER(self):
        return self.parameter['max_temper']

    def MAX_SPEED(self):
        return  self.parameter['max_speed']


if __name__ == '__main__':
    param = Parameter(os.getcwd() + '/Parameter.json')
    print(param.getIP())



